package com.xiaoshuidi.cloud.module.rooms.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.reservation.vo.GetByUserIdRespVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.reservation.vo.ReserveBillPageReq;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.reservation.vo.ReserveBillPageResp;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomHouseReserve;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * <p>
 * 房源预定信息 Mapper 接口
 * </p>
 *
 * @author Feng
 * @since 2023-05-15
 */
@Mapper
public interface RoomHouseReserveMapper extends BaseMapper<RoomHouseReserve> {

    @Select("select " +
            "       rhr.id id, " +
            "       rhr.bill_no              billNo, " +
            "       case " +
            "           when rh.is_concentrated = 1 then concat(rh.area, rh.building_no, '栋', rh.name) " +
            "           else concat(rh.area, rh.building_no, '栋', rh.unit_no, '单元', rh.name) " +
            "           end                  roomHouse, " +
            "       rhr.booker               booker, " +
            "       rhr.company_name         companyName, " +
            "       rhr.deposit              billAmount, " +
            "       rhr.deposit_payment_term depositPaymentTerm, " +
            "       rhr.payment_status       paymentStatus, " +
            "       rhr.status               status, " +
            "       rhr.booker_phone         bookerPhone, " +
            "       rhr.check_in_date        checkInDate, " +
            "       rhr.create_time        createTime, " +
            "       rhr.room_house_id        roomHouseId, " +
            "       rhr.expiration_date     expirationDate "+
            "from room_house_reserve rhr " +
            "         left join room_house rh on rhr.room_house_id = rh.id " +
            "where ${ew.sqlSegment}")
    Page<ReserveBillPageResp> billPage(Page<?> page, @Param("ew") QueryWrapper<?> wrapper);

    @Select("select " +
            "      count(rhr.id) " +
            "from room_house_reserve rhr " +
            "         left join room_house rh on rhr.room_house_id = rh.id " +
            "where ${ew.sqlSegment}")
    Long billCount(@Param("ew") QueryWrapper<?> wrapper);

    @Select("SELECT " +
            "rhr.`id` as id , " +
            "ra.`name` as apartmentName , " +
            "rh.area as area," +
            "rh.building_no as buildingNo, " +
            "rh.unit_no as unitNo , " +
            "rh.is_concentrated as isConcentrated, " +
            "rh.floor_no as floorNo, " +
            "rh.`name` AS roomHouseName, " +
            "rhr.deposit as deposit, " +
            "rhr.create_time as createTime, " +
            "rhr.expiration_date as expirationDate, " +
            "rhr.deposit_payment_term as depositPaymentTerm, " +
            "rhr.check_in_date as checkInDate, " +
            "rhr.STATUS as status, " +
            "rhr.payment_status as paymentStatus " +
            "FROM " +
            "`room_house_reserve` rhr " +
            "LEFT JOIN room_house rh ON rhr.room_house_id = rh.id " +
            "LEFT JOIN room_apartment ra ON rh.apartment_id = ra.id where ${ew.sqlSegment}")
    Page<GetByUserIdRespVO> getByBookerId(@Param("page") Page<RoomHouseReserve> page, @Param("ew") QueryWrapper<RoomHouseReserve> userId);

    @Update("update room_house_reserve set booker_phone=#{mobile} where booker_id=#{userId} ")
    void updateMember(@Param("userId") Long userId, @Param("mobile") String mobile);


    IPage<ReserveBillPageResp> pageWithdrawBillList(Page<ReserveBillPageResp> page , @Param("param") ReserveBillPageReq param);
}
